package com.shihb.chapter06customercrm.service

import com.shihb.chapter06customercrm.bean.Customer

import scala.collection.mutable.ListBuffer

/**
 * Description:
 *
 * @author shihb
 * @date 2020/10/2 16:08
 */
class CustomerService {
  //id: Int, name: String, gender: Char, age: Short, tel: String, email: String
  val customers=ListBuffer(new Customer(1,"shb",'男',20,"16699194123","shb@qq.com"))
  var customerNum=1
  def list: ListBuffer[Customer] ={
    this.customers
  }
  def add(c:Customer): Boolean = {
    c.id=customerNum
    customerNum+=1
    customers+=c
    true
  }
  def delete(id:Int): Boolean ={
    findIndexById(id) match {
      case -1 => false
      case index =>customers.remove(index);true
    }
  }
  def findIndexById(id:Int): Int ={
    import util.control.Breaks._
    customers
    //默认索引
    var index = -1
    breakable {
      for(i<- 0 until customers.length){
        if (customers(i).id==id){
          index=i
          break()
        }
      }
    }
    index
  }

}
